const { contextBridge, ipcRenderer } = require('electron')
const path = require('path')

// 暴露安全的API给渲染进程
contextBridge.exposeInMainWorld('electronAPI', {
  // 文件系统操作
  readFile: (filePath) => ipcRenderer.invoke('read-file', filePath),
  writeFile: (filePath, content) => ipcRenderer.invoke('write-file', filePath, content),
  readDirectory: (dirPath) => ipcRenderer.invoke('read-directory', dirPath),
  copyFile: (sourcePath, targetPath) => ipcRenderer.invoke('copy-file', sourcePath, targetPath),

  // 对话框
  showSaveDialog: (options) => ipcRenderer.invoke('show-save-dialog', options),
  showOpenDialog: (options) => ipcRenderer.invoke('show-open-dialog', options),
  showMessageBox: (options) => ipcRenderer.invoke('show-message-box', options),

  // 应用信息
  getAppVersion: () => ipcRenderer.invoke('get-app-version'),
  getAppPath: (name) => ipcRenderer.invoke('get-app-path', name),

  // 外部链接
  openExternal: (url) => ipcRenderer.invoke('open-external', url),

  // 事件监听
  onMenuAction: (callback) => {
    const channels = [
      'open-history-directory',
      'export-search-results',
      'open-settings',
      'focus-search',
      'open-advanced-search',
      'clear-search-history',
      'toggle-theme',
      'open-performance-monitor',
      'clear-cache',
      'show-shortcuts',
      'show-about',
      'app-before-quit'
    ]

    channels.forEach(channel => {
      ipcRenderer.on(channel, (event, ...args) => callback(channel, ...args))
    })

    // 返回清理函数
    return () => {
      channels.forEach(channel => {
        ipcRenderer.removeAllListeners(channel)
      })
    }
  },

  // 更新相关事件
  onUpdateEvent: (callback) => {
    const updateChannels = [
      'update-available',
      'download-progress',
      'update-downloaded'
    ]

    updateChannels.forEach(channel => {
      ipcRenderer.on(channel, (event, ...args) => callback(channel, ...args))
    })

    return () => {
      updateChannels.forEach(channel => {
        ipcRenderer.removeAllListeners(channel)
      })
    }
  },

  // 移除监听器
  removeAllListeners: (channel) => {
    ipcRenderer.removeAllListeners(channel)
  }
})

// 暴露Node.js路径工具
contextBridge.exposeInMainWorld('pathAPI', {
  join: (...paths) => path.join(...paths),
  dirname: (pathStr) => path.dirname(pathStr),
  basename: (pathStr, ext) => path.basename(pathStr, ext),
  extname: (pathStr) => path.extname(pathStr),
  resolve: (...paths) => path.resolve(...paths),
  normalize: (pathStr) => path.normalize(pathStr),
  isAbsolute: (pathStr) => path.isAbsolute(pathStr),
  sep: path.sep
})

// 暴露平台信息
contextBridge.exposeInMainWorld('platformAPI', {
  platform: process.platform,
  arch: process.arch,
  version: process.version,
  isWindows: process.platform === 'win32',
  isMacOS: process.platform === 'darwin',
  isLinux: process.platform === 'linux'
})

// 开发环境下的调试工具
if (process.env.NODE_ENV === 'development') {
  contextBridge.exposeInMainWorld('devAPI', {
    openDevTools: () => ipcRenderer.send('open-dev-tools'),
    reload: () => ipcRenderer.send('reload'),
    log: (...args) => console.log('[Renderer]', ...args)
  })
}